//
//  XNScrapViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/16.
//

#import "XNScrapViewController.h"
#import "XNScrapBillInfoModel.h"
#import "XNScrapApproveRequest.h"
#import "XNScrapBillInfoRequest.h"

@interface XNScrapViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNScrapBillInfoModel *infoModel;

@end

@implementation XNScrapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNScrapBillInfoRequest *infoRequest = [[XNScrapBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNScrapBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNScrapBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNScrapApproveRequest *approveRequest = [[XNScrapApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNScrapApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNScrapApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}



- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"报废信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                               [NSString stringWithFormat:@"报废原因： %@",self.infoModel.scrapReason],
                               nil];
    itemModel2.showItemArray = showItemArray2;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"子表信息";
    [self.infoModel.itemList enumerateObjectsUsingBlock:^(XNScrapItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"资产编号: %@",obj.fixedAssetId],
                                       [NSString stringWithFormat:@"资产名称： %@",obj.assetObject.assetName],
                                       [NSString stringWithFormat:@"规格型号： %@",obj.assetObject.modelNumber],
                                       [NSString stringWithFormat:@"启用日期： %@",obj.assetObject.startDateStr],
                                   [NSString stringWithFormat:@"原始成本: %.3f",obj.assetObject.originalValue],
                                   [NSString stringWithFormat:@"折旧年限： %.3f",obj.depreciationYear],
                                   [NSString stringWithFormat:@"每月折旧：  %.3f",obj.depreciationMonth],
                                   [NSString stringWithFormat:@"累计折旧：  %.3f",obj.depreciationTotal],
                                   [NSString stringWithFormat:@"残值: %.3f",obj.leftValue],
                                   [NSString stringWithFormat:@"备注: %@",obj.remark.length > 0 ? obj.remark : @""],
                                       nil];
        itemModel3.showItemArray = showItemArray3;
        [showModel3.showDataArray addObject:itemModel3];
    }];
    [self.showArray addObject:showModel3];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}


@end
